package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 保存的按钮
     */
    public void saveOrUpdate(Product product) {
        try {
            if (product.getId() == null) {
                productMapper.insert(product);
            } else {
                productMapper.updateByPrimaryKey(product);
            }
        } catch (Exception e) {
            throw new RuntimeException("更新失败");
        }
    }

    public List<Product> listAll() {
        return productMapper.selectAll();
    }

    public Product selectById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    //回显所有商品
    @Override
    public PageInfo query(ProductQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Product> list = productMapper.selectForListByType(qo);
        return new PageInfo(list);
    }


}
